package com.sz.lk.wingsystem.ui;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.sz.lk.wingsystem.MainApp;
import com.sz.lk.wingsystem.R;
import com.sz.lk.wingsystem.bean.TaskResult;
import com.sz.lk.wingsystem.bean.TaskReturnResult;
import com.sz.lk.wingsystem.mutils.ToastUtils;
import com.sz.lk.wingsystem.mvp.BaseActivity;
import com.sz.lk.wingsystem.mvp.ModelPath;
import com.sz.lk.wingsystem.mvp.contract.TaskContract;
import com.sz.lk.wingsystem.mvp.presenter.TaskPreserent;
import com.sz.lk.wingsystem.widgets.ClearableEditText;

import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author : liuke
 * e-mail : 1012459343@qq.com
 * date   : 2020/12/7
 * desc   :
 * version: 1.0
 */
public class CheckPage extends BaseActivity<TaskPreserent> implements TaskContract.view {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.code_search)
    ClearableEditText codeSearch;
    @BindView(R.id.a)
    RelativeLayout a;
    @BindView(R.id.clear_all)
    TextView clearAll;
    @BindView(R.id.k_code)
    TextView kCode;
    @BindView(R.id.k_weight)
    ClearableEditText kWeight;
    @BindView(R.id.k_volume)
    ClearableEditText kVolume;
    @BindView(R.id.k_length)
    ClearableEditText kLength;
    @BindView(R.id.k_width)
    ClearableEditText kWidth;
    @BindView(R.id.k_height)
    ClearableEditText kHeight;
    @BindView(R.id.f)
    TextView f;
    @BindView(R.id.fen_kou)
    TextView fenKou;
    @BindView(R.id.cs)
    TextView cs;
    @BindView(R.id.fen_shi)
    TextView fenShi;
    @BindView(R.id.ms)
    TextView ms;
    @BindView(R.id.desc)
    TextView desc;
    @BindView(R.id.up_bt)
    Button upBt;
    private String id = "";
    private String code_temp = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initEventAndData() {
        codeSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
                    ToastUtils.show(codeSearch.getText().toString().trim());
                    //mPresenter.getTask(editable.toString().trim());
                    //扫描显示到下面code_temp
                    code_temp = codeSearch.getText().toString().trim();
                    String temp = codeSearch.getText().toString().trim();
                    codeSearch.setText("");
                    kCode.setText(code_temp);
                    getTask(temp);
                }
                return true;
            }
        });
    }

    @Override
    protected TaskPreserent initPresenter() {
        return new TaskPreserent();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_check_page;
    }

    @Override
    public void onTaskSuccess(TaskResult bean) {
        if (bean.isIsSucceed()) {
            kCode.setText(code_temp);
            id = bean.getResult().getData().getId();
            kWeight.setText(bean.getResult().getData().getWeight() + "");
            kVolume.setText(bean.getResult().getData().getVolume() + "");
            kLength.setText(bean.getResult().getData().getLength() + "");
            kHeight.setText(bean.getResult().getData().getHeight() + "");
            kWidth.setText(bean.getResult().getData().getWidth() + "");
            fenKou.setText(bean.getResult().getData().getEstimateChannel());
            fenShi.setText(bean.getResult().getData().getEstimateAddress());
            desc.setText(bean.getRemark());
        } else {
            kWeight.setText("");
            kVolume.setText("");
            kLength.setText("");
            kHeight.setText("");
            kWidth.setText("");
            fenKou.setText("");
            fenShi.setText("");
            desc.setText(bean.getRemark());
            kCode.setText(code_temp);
        }
    }

    @Override
    public void onUploadTaskInfoSuccess(TaskReturnResult taskReturnResult) {
        if (taskReturnResult.isIsSucceed()) {
            ToastUtils.show("上传成功！");
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void showWaitingDialog(String tip) {

    }

    @Override
    public void hideWaitingDialog() {

    }

    @OnClick({R.id.back, R.id.clear_all, R.id.up_bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.clear_all:
                kWeight.setText("");
                kVolume.setText("");
                kLength.setText("");
                kHeight.setText("");
                kWidth.setText("");
                break;
            case R.id.up_bt:
                /*String id, String code, double weight, double volume,
                double length, double width, double height,
                String estimateChannel, String estimateAddress*/
                String code = Objects.requireNonNull(kCode.getText()).toString().trim();
                double weight = Double.parseDouble(Objects.requireNonNull(kWeight.getText()).toString().trim());
                double volume = Double.parseDouble(Objects.requireNonNull(kVolume.getText()).toString().trim());
                double length = Double.parseDouble(Objects.requireNonNull(kLength.getText()).toString().trim());
                double width = Double.parseDouble(Objects.requireNonNull(kWidth.getText()).toString().trim());
                double height = Double.parseDouble(Objects.requireNonNull(kHeight.getText()).toString().trim());
                String estimateChannel = fenKou.getText().toString();
                String estimateAddress = fenShi.getText().toString();
                String strUTF8 = null;
                try {
                    strUTF8 = URLDecoder.decode(estimateAddress, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                Log.e("dadsdsdsd", estimateChannel);
                Log.e("dadsdsdsd", estimateAddress);
                //mPresenter.getUploadTaskInfo(id, code, weight, volume, length, width, height, estimateChannel, estimateAddress);
                getUploadTaskInfo(id, code, weight, volume, length, width, height, estimateChannel, strUTF8);
                break;
            default:
                break;
        }
    }

    public void getTask(String code) {
        StringRequest stringRequest = new StringRequest(Request.Method.GET,
                ModelPath.baseUrl_NO_Splite + "WMSTask/LoadTaskInfoByCode?code=" + code,
                response -> {
                    try {
                        onTaskSuccess(new Gson().fromJson(response, TaskResult.class));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }, error -> {
            kCode.setText(code_temp);
        });
        MainApp.mQueue.add(stringRequest);
    }

    public void getUploadTaskInfo(String id, String code, double weight, double volume,
                                  double length, double width, double height,
                                  String estimateChannel, String estimateAddress) {
        Map<String, Object> merchant = new HashMap<>();
        merchant.put("id", id);
        merchant.put("code", code);
        merchant.put("weight", weight);
        merchant.put("volume", volume);
        merchant.put("length", length);
        merchant.put("width", width);
        merchant.put("height", height);
        merchant.put("estimateChannel", estimateChannel);
        merchant.put("estimateAddress", estimateAddress);
        JSONObject jsonObject = new JSONObject(merchant);
        JsonRequest<JSONObject> jsonRequest = new JsonObjectRequest(Request.Method.POST, ModelPath.baseUrl_NO_Splite + "WMSTask/UploadTaskInfo", jsonObject,
                response -> {
                    String a = response.toString();
                    Log.e("dadsdsdsd", a);
                    onUploadTaskInfoSuccess(new Gson().fromJson(a, TaskReturnResult.class));
                }, error -> {
            ToastUtils.show(error.getMessage());
        }) {

            @Override
            public Map<String, String> getHeaders() {
                HashMap<String, String> headers = new HashMap<>();
                headers.put("Content-Type", "application/json");
                return headers;
            }
        };
        MainApp.mQueue.add(jsonRequest);
    }
}